<?php
class Events {
	var $htmlString;
	var $rssString;
		
	function getEventListRSS() {
		global $dbname, $db, $db_selected;
		$today = date('Y-m-d');
		
		$sql = "SELECT * from cdc_Calendar 
				WHERE
				(StartDate >= '$today' OR EndDate >= '$today')
				AND Public = 1 
				AND Active = 1
				ORDER BY StartDate DESC";
		$result = mysql_db_query($dbname,$sql);
		
		while ($value = mysql_fetch_array($result)) {
			$eventID    = $value['EventID'];
			$eventTitle = stripslashes($value['EventName']);
			$eventDesc  = fieldToDB($value['EventDescription']);
			$eventDescShort = shortText($eventDesc,20);
			$register   = $value['RegistrationRequired'];
			$regURL     = $value['RegistrationLink'];
			
			$dateUpdate = new Zend_Date($value['DateUpdate'],'en');
			$newDateUpdate = $dateUpdate->toString('EEE, d MMM YYYY 00:00:00 GMT');
			
			$this->rssString .= '<item>';
			$this->rssString .= '<title>'.$eventTitle.'</title>';
			$this->rssString .= '<link>http://www.childdisabilityconnection.org/events.php?eventID='.$eventID.'</link>';
			$this->rssString .= '<pubDate>'.$newDateUpdate.'</pubDate>';
			$this->rssString .= '<description>'.$eventDesc.'</description>';
			$this->rssString .= '</item>';			
		}	
		return $this->rssString;
	}
	
	function getEventList($dateStart,$public=null,$view='page',$month=null,$limit=null) {
		global $dbname, $db, $db_selected;
		global $monthNameLongArray;
		global $translate;
		$thisYear = date('Y');
		$today = date('Y-m-d');
                
           if ($limit>0) {
               $limit = 'LIMIT '.$limit;
           } else {
             $limit = '';
           }
		
		if ($public != null) {
			$whereP = "AND Public = $public";
		}
		
		if (!empty($month)) {
			$whereD = "MONTHNAME(StartDate) = '$month' AND YEAR(StartDate) = '$thisYear' AND StartDate >= '".date('Y-m-d')."'";
		} else {
			$whereD = "(StartDate >= '$dateStart' OR EndDate >= '$dateStart')";
		}
		$whereD .= ' AND Active = 1 AND Deleted = 0 ';
		if ($view == 'page') {
			$acdc = 'DESC';
		} else {
			$acdc = 'ASC';
		}
		
		$sql = "SELECT * from cdc_Calendar 
				WHERE
				$whereD
				$whereP
				ORDER BY StartDate $acdc $limit
				";
          $result = mysql_db_query($dbname,$sql);
          $rows = mysql_num_rows($result);
		
		if ($view == 'window') {
			$this->htmlString .= '<ul class="orangeUL">';
		}
		
		$j=0;
		while ($value = mysql_fetch_array($result)) {
			$eventID    = $value['EventID'];
			$eventTitle = stripslashes($value['EventName']);
			$eventDesc  = stripslashes($value['EventDescription']);
			$eventDesc  = shortText($eventDesc,20);
			$register   = $value['RegistrationRequired'];
			$regURL     = $value['RegistrationLink'];
						
			/*
			 * Format dates
			 */
			$dateStart = new Zend_Date($value['StartDate'], Zend_Date::ISO_8601, 'en_US');
			$newDateStart = $dateStart->toString('MMM d');
			
			if ($value['EndDate'] != '0000-00-00 00:00:00') {
				$dateEnd = new Zend_Date($value['EndDate'],'en');
				$newDateEnd = $dateEnd->toString('MMM d');
			} else {
				$newDateEnd = '';
			}
			
			/*
			 * Has this event expired?
			 */
			$today = new Zend_Date();
			$checkDate = new Zend_Date($value['StartDate'],'en');
			if ($checkDate->isEarlier($today)) {
				$expired = 1;
			}
			
			if (($j%2)>0) {
				$rowClass = 'altRow';
			} else {
				$rowClass = '';
			}
			
			if ($view == 'page') {
				$this->htmlString .= '<div class="'.$rowClass.'" style="padding: 8px;">';
				$this->htmlString .= '<div style="width: 70%; float: left;"><p><a href="events.php?eventID='.$eventID.'" class="bigLink" title="View event details">'.$eventTitle.'</a></p></div>';
				$this->htmlString .= '<div style="width: 30%; float: right;"><h3 style="text-align: right;">';
				$this->htmlString .= $newDateStart;
				if (!empty($newDateEnd)) {
					$this->htmlString .= ' to '.$newDateEnd;
				}
				$this->htmlString .= '</h3></div>';
				$this->htmlString .= '<div style="clear: both;"></div>';
				if (!empty($eventDesc)) {
					$this->htmlString .= '<p style="padding-top: 3px;">'.$eventDesc.'</p>';
				}
				if ( (($register == 1) || !empty($regURL)) && ($expired != 1)) {
					if (empty($regURL)) {
						$regURL = 'accountCreate.php?itemType=event&itemID='.$eventID;
					}
					$this->htmlString .= '<div style="float: right;"><p><a href="'.$regURL.'" class="iconRegister" title="Register for this event">Register for this event</a></p></div><div style="clear: right;"></div>';
				}
				$this->htmlString .= '</div>';
			} elseif ($view == 'window') {
				$this->htmlString .= '<li><a href="events.php?eventID='.$eventID.'">'.$eventTitle.'</a> '.$newDateStart.'</li>';
			}	
			$j++;
		}	
		if ($view == 'window') {
			$this->htmlString .= '</ul>';
		}
		
	}
	function getEventDetails($eventID,$render='screen') {		
		global $dbname, $db, $db_selected;
		
		$sql = "SELECT * from cdc_Calendar WHERE EventID = '$eventID'";		
		$result = mysql_db_query($dbname,$sql);
		$rowEvent = mysql_fetch_assoc($result);
		
		/*
		 * Process data
		 */
		$eTitle       = stripslashes($rowEvent['EventName']);
		$eDesc        = stripslashes($rowEvent['EventDescription']);
		$eURL         = $rowEvent['ContactURL'];
		$eRegRequired = $rowEvent['RegistrationRequired'];
		$eRegURL      = $rowEvent['RegistrationLink']; 
		
		/*
		 * Check for properly formatted URL's
		 */
		if (!empty($eURL)) {
			if (substr($eURL,0,7) != 'http://') {
				$eURL = 'http://'.$eURL;
			}
		}
		if (!empty($eRegURL)) {
			if (substr($eRegURL,0,7) != 'http://') {
				$eRegURL = 'http://'.$eRegURL;
			}
		}
		
		/*
		 * Convert dates
		 */
		$dateStart    = new Zend_Date($rowEvent['StartDate'], Zend_Date::ISO_8601, 'en_US');
		$newDateStart = $dateStart->toString('MMM d, h:m a');
		if ($rowEvent['EndDate'] != '0000-00-00 00:00:00') {
			$dateEnd      = new Zend_Date($rowEvent['EndDate'], Zend_Date::ISO_8601, 'en_US');
			$newDateEnd   = $dateEnd->toString('MMM d, h:m a');
		} else {
			$newDateEnd = '';
		}
		
		$dateString = $newDateStart;
		if (!empty($newDateEnd)) {
			$dateString .= ' to '.$newDateEnd;
		}
		$rowEvent['DateString'] = $dateString;
		
		/*
		 * Has this event expired?
		 */
		$today = new Zend_Date();
		$checkDate = new Zend_Date($rowEvent['StartDate'],'en');
		if ($checkDate->isEarlier($today)) {
			$expired = 1;
			$rowEvent['Expired'] = $expired;
		}
		
		/*
		 * Format location
		 */
		$location = '';
		if (!empty($rowEvent['Location'])) {
			$location .= $rowEvent['Location'].'<br />';
		}
		if (!empty($rowEvent['LocationAddress'])) {
			$location .= $rowEvent['LocationAddress'].'<br />';
		}
		if (!empty($rowEvent['LocationCity'])) {
			$location .= $rowEvent['LocationCity'];
		}
		if (!empty($rowEvent['LocationState']) && $rowEvent['LocationState'] != '--') {
			$location .= ' '.$rowEvent['LocationState'];
		}
		if (!empty($rowEvent['LocationZip'])) {
			$location .= ' '.$rowEvent['LocationZip'];
		}
		$rowEvent['LocationString'] = $location;
		
		/*
		 * Try to make Google location string
		 */
		if (	
			!empty($rowEvent['LocationCity'])  && 
			!empty($rowEvent['LocationState']) && 
			!empty($rowEvent['LocationAddress'])) {
			
			$googleLocation = $rowEvent['LocationAddress'].' '.$rowEvent['LocationCity'].' '.$rowEvent['LocationState'].' '.$rowEvent['LocationZip'];	
		} else {
			$googleLocation = 0;
		}
		
		/*
		 * Format contact string
		 */
		$contact = '';
		if (!empty($eURL)) {
			$contact .= '<p><a href="'.$eURL.'" title="Event website" class="iconWebsite">Event website</a></p>';
		}
		if (!empty($rowEvent['ContactPerson'])) {
			$contact .= '<p class="iconPerson">'.$rowEvent['ContactPerson'].'</p>';
		}
		if (!empty($rowEvent['ContactPhone'])) {
			$contact .= '<p class="iconPhone">'.$rowEvent['ContactPhone'].'</p>';
		}
		if (!empty($rowEvent['ContactEmail'])) {
			$contact .= '<p><a href="mailto:'.$rowEvent['ContactEmail'].'" title="Email contact" class="iconEmail">'.$rowEvent['ContactEmail'].'</a></p>';
		}
		$rowEvent['ContactString'] = $contact;
		
		if ($render == 'screen') {
			/*
			 * Put everything together and return HTML string 
			 */
			$this->htmlString .= '<div style="width: 50%; float: left;"><p><h2 style="margin: 0;">'.$eTitle.'</h2></p><h3 class="doPrint">'.$dateString.'</h3></div>';
			$this->htmlString .= '<div style="width: 50%; float: right;"><p class="noPrint"><h3 style="text-align: right;">';
			$this->htmlString .= $dateString;
			$this->htmlString .= '</p></h3></div><div style="clear: both; height: 10px;"></div>';
			if (!empty($eDesc)) {
				$this->htmlString .= '<p>'.$eDesc.'</p>';
			}
			$this->htmlString .= '<div style="height: 10px;"></div>';
			
			/*
			 * Display registration link
			 */
			if ( (($rowEvent['RegistrationRequired'] == 1) || !empty($eRegURL)) && ($expired != 1) ) {
				if (empty($eRegURL)) {
					$eRegURL = 'eventRegistration.php?eventID='.$eventID;
				}
				$this->htmlString .= '<p class="noPrint"><a href="'.$eRegURL.'" class="iconRegister bigLink" title="Register for this event">Register for this event</a></p>'; 
				$this->htmlString .= '<p class="doPrint" style="margin-bottom: 10px;"><b>Register for this event at: </b>'.SITE_URL.'/'.$eRegURL.'</p>';
			}
			
			/*
			 * For more information
			 */
			if (!empty($contact)) {
				$this->htmlString .= '<div style="float: left; width: 50%; vertical-align: top;">';
				$this->htmlString .= '<h3 style="margin-bottom: 10px;">For more information:</h3><p>';
				$this->htmlString .= $contact.'</p></div>';
			}
			if (!empty($location)) {
				$this->htmlString .= '<div style="float: left; width: 50%; vertical-align: top;">';
				$this->htmlString .= '<h3 style="margin-bottom: 10px;">Event location:</h3><p>';
				$this->htmlString .= $location.'</p></div>';
			}
			$this->htmlString .= '<div style="clear: left;"></div>'."\n";

			if ($googleLocation != 0) {		
				$pointerText = '<p><b>'.$eTitle.'</b><br />'.$location.'</p>';
				$this->htmlString .= '<div id="mapDiv"></div>'."\n";
				$this->htmlString .= '<script type="text/javascript">';
				$this->htmlString .= "showGoogleMap('".$googleLocation."','mapDiv','".$pointerText."');";
				$this->htmlString .= '</script>';
			}			
			
			return $this->htmlString;		
		} elseif ($render == 'array') {
			return $rowEvent;
		}		
	}
	function renderEventMonthLinks($public=null) {
		global $dbname, $db, $db_selected;
		global $monthNameLongArray;
		global $translate;
				
		$thisYear  = date('Y');
		$thisMonth = date('n'); 
		
		if (!empty($public)) {
			$whereP = 'AND Public = '.$public;
		}
		
		$monthLinkString = '<ul class="grayUL" style="margin-left: 20px;">';
		for($a=0;$a<=11;$a++) {
			$sql = "SELECT EventID from cdc_Calendar 
					WHERE 
					MONTHNAME(StartDate) = '".$monthNameLongArray[$a]."' AND
					YEAR(StartDate) = '".$thisYear."' AND
					Active  = 1 AND
					Deleted = 0
					$whereP";
			$result = mysql_db_query($dbname,$sql);
			$events = mysql_num_rows($result);
			
			if ($events>0) {
				$transMonth = $translate->_($monthNameLongArray[$a]);
				if($a<($thisMonth-1)) {
					$monthLinkString .= '<li><a href="events.php?month='.$monthNameLongArray[$a].'" class="grayText">'.$transMonth.'</a></li>';
				} else {
					$monthLinkString .= '<li><a href="events.php?month='.$monthNameLongArray[$a].'"><b>'.$transMonth.'</b></a></li>';
				}	
			}	
		}
		$monthLinkString .= '</ul>';
		return $monthLinkString;		
	}
	function checkRegistration($memberID,$eventID) {
		global $dbname, $db, $db_selected;
		
		$sql = "SELECT * from cdc_linkEventMember 
				WHERE 
				MemberID = '$memberID' AND
				EventID  = '$eventID'";
		$result = mysql_db_query($dbname,$sql);
		if (mysql_num_rows($result)>0) {
			return true;
		} else {
			return false;
		}
	}
	
	function getRegisteredEventsByMember($memberID) {
		
	}
	
	function renderEventHTML() {
		return $this->htmlString;
	}
}
?>